package com.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.entity.Reward;
import com.demo.entity.Schoolproject;
import com.demo.mapper.SchoolprojectMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 校级项目管理
 */
@RestController
@RequestMapping("/schoolproject")
public class SchoolprojectController {
  /**
   * 服务对象
   */
  @Resource
  SchoolprojectMapper schoolprojectMapper;
  
  // 新增
  @PostMapping
  public Result<?> insert(@RequestBody Schoolproject schoolproject) {
    schoolprojectMapper.insert(schoolproject);
    return Result.success();
  }
  
  //  删除 ById
  @DeleteMapping("/{id}")
  public Result<?> delete(@PathVariable Integer id) {
    schoolprojectMapper.deleteById(id);
    return Result.success();
  }
  
  // 修改
  @PutMapping
  public Result<?> updata(@RequestBody Schoolproject schoolproject) {
    schoolprojectMapper.updateById(schoolproject);
    return Result.success();
  }
  
  /**
   * 分页,默认1页,10个数据,  默认根据 Sno 项目编号  查询
   *
   * @param pageNum  页数
   * @param pageSize 页面显示数
   * @param search   搜索关键词
   * @param sort     搜索类别
   * @return
   */
  @GetMapping
  public Result<?> findPageByaName(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam(defaultValue = "Sno") String sort) {
    LambdaQueryWrapper<Schoolproject> wrapper = Wrappers.<Schoolproject>lambdaQuery();
    if (StrUtil.isNotBlank(search)) {
      switch (sort) {
        case "Sno": //根据项目编号
          wrapper.like(Schoolproject::getSno, search);
          break;
        case "Sname"://项目名称
          wrapper.like(Schoolproject::getSname, search);
          break;
        case "Pname"://课题名称
          wrapper.like(Schoolproject::getPname, search);
          break;
        case "Sleader"://负责人
          wrapper.like(Schoolproject::getSleader, search);
          break;
        case "Ranks"://职务或职称
          wrapper.like(Schoolproject::getRanks, search);
          break;
        case "Sdepartment"://所在部门
          wrapper.like(Schoolproject::getSdepartment, search);
          break;
          
      }
    }
    
    Page<Schoolproject> AdminPage = schoolprojectMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(AdminPage);
  }
  
  
}

